package pprg;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

class Util {

	private static final String COMMA_DELIMITER = ";";
	private static final String NEW_LINE_SEPERATOR = "\n";

	public static final String FILE_HEADER = "# philosopers;# calculations; # mean time";
	public static final String FILE_HEADER_EATING_EVALUATION = "# philosopers;# calculations; avg eating time;avg eating count";

	public static int readInput(String infoMsg, String errorMsg) {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		Boolean error = false;
		String input = "";
		int value = 0;
		do {
			System.out.print(infoMsg);
			try {
				input = br.readLine();
				value = Integer.parseInt(input);
				error = false;
			} catch (Exception e) {
				error = true;
				System.err.println(errorMsg);
			}
		} while (error == true);
		return value;
	}

	public static void writeEatingTimesEvaluationToCsvFile(String filename,
			Map<Integer, Long> avgEatingTimes,
			Map<Integer, Integer> avgEatingAmounts, int numberOfCalculations) {

		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(filename);
			fileWriter.append(FILE_HEADER_EATING_EVALUATION);
			fileWriter.append(NEW_LINE_SEPERATOR);

			for (Integer i : avgEatingAmounts.keySet()) {
				fileWriter.append(String.valueOf(i));
				fileWriter.append(COMMA_DELIMITER);
				fileWriter.append(String.valueOf(numberOfCalculations));
				fileWriter.append(COMMA_DELIMITER);
				fileWriter.append(String.valueOf(avgEatingTimes.get(i)));
				fileWriter.append(COMMA_DELIMITER);
				fileWriter.append(String.valueOf(avgEatingAmounts.get(i)));
				fileWriter.append(NEW_LINE_SEPERATOR);
			}
			System.out.println("CSV file was created succesfully");
		} catch (Exception e) {
			System.out.println("ERROR while writint to CSV file");
		} finally {
			try {
				fileWriter.flush();
				fileWriter.close();
			} catch (IOException e) {
				System.out
						.println("Error while flushing/closing fileWriter !!!");
				e.printStackTrace();
			}
		}

	}

	public static void writeDeadlockTimesToCsvFile(String filename, Map<Integer, Long> times,
			int numberOfCalculations) {

		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(filename);

			for (Integer i : times.keySet()) {
				fileWriter.append(String.valueOf(i));
				fileWriter.append(COMMA_DELIMITER);
				fileWriter.append(String.valueOf(numberOfCalculations));
				fileWriter.append(COMMA_DELIMITER);
				fileWriter.append(String.valueOf(times.get(i)));
				fileWriter.append(NEW_LINE_SEPERATOR);
			}
			System.out.println("CSV file was created succesfully");
		} catch (Exception e) {
			System.out.println("ERROR while writint to CSV file");
		} finally {
			try {
				fileWriter.flush();
				fileWriter.close();
			} catch (IOException e) {
				System.out
						.println("Error while flushing/closing fileWriter !!!");
				e.printStackTrace();
			}
		}
	}
}
